package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.domain.InsReagentBottle;
import com.ruoyi.project.product.domain.InsReagentBox;
import com.ruoyi.project.product.service.IInsReagentBottleService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 试剂瓶记录Controller
 *
 * @author taney
 * @date 2020-11-25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/reagentBottle")
public class InsReagentBottleController extends BaseController {

    private final IInsReagentBottleService iInsReagentBottleService;

    /**
     * 查询试剂瓶记录列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBottle:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsReagentBottle insReagentBottle) {
        startPage();
        LambdaQueryWrapper<InsReagentBottle> lqw = new LambdaQueryWrapper<InsReagentBottle>();
        if (insReagentBottle.getBoxId() != null) {
            lqw.eq(InsReagentBottle::getBoxId, insReagentBottle.getBoxId());
        }
        if (insReagentBottle.getOrderListId() != null) {
            lqw.eq(InsReagentBottle::getOrderListId, insReagentBottle.getOrderListId());
        }
        if (insReagentBottle.getOrderId() != null) {
            lqw.eq(InsReagentBottle::getOrderId, insReagentBottle.getOrderId());
        }
        if (StringUtils.isNotBlank(insReagentBottle.getProjectCode())) {
            lqw.eq(InsReagentBottle::getProjectCode, insReagentBottle.getProjectCode());
        }
        if (StringUtils.isNotBlank(insReagentBottle.getProjectName())) {
            lqw.like(InsReagentBottle::getProjectName, insReagentBottle.getProjectName());
        }
        if (StringUtils.isNotBlank(insReagentBottle.getTestMethod())) {
            lqw.eq(InsReagentBottle::getTestMethod, insReagentBottle.getTestMethod());
        }
        if (StringUtils.isNotBlank(insReagentBottle.getExpireDate())) {
            lqw.eq(InsReagentBottle::getExpireDate, insReagentBottle.getExpireDate());
        }
        if (StringUtils.isNotBlank(insReagentBottle.getLotNo())) {
            lqw.eq(InsReagentBottle::getLotNo, insReagentBottle.getLotNo());
        }
        if (StringUtils.isNotBlank(insReagentBottle.getBarcode())) {
            lqw.eq(InsReagentBottle::getBarcode, insReagentBottle.getBarcode());
        }
        if (StringUtils.isNotBlank(insReagentBottle.getPrintState())) {
            lqw.eq(InsReagentBottle::getPrintState, insReagentBottle.getPrintState());
        }
        lqw.orderByDesc(InsReagentBottle::getOrderId);
        List<InsReagentBottle> list = iInsReagentBottleService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出试剂瓶记录列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBottle:export')")
    @Log(title = "试剂瓶记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsReagentBottle insReagentBottle) {
        LambdaQueryWrapper<InsReagentBottle> lqw = new LambdaQueryWrapper<InsReagentBottle>(insReagentBottle);
        List<InsReagentBottle> list = iInsReagentBottleService.list(lqw);
        ExcelUtil<InsReagentBottle> util = new ExcelUtil<InsReagentBottle>(InsReagentBottle.class);
        return util.exportExcel(list, "reagentBottle");
    }

    /**
     * 获取试剂瓶记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBottle:query')")
    @GetMapping(value = "/{bottleId}")
    public AjaxResult getInfo(@PathVariable("bottleId") Integer bottleId) {
        return AjaxResult.success(iInsReagentBottleService.getById(bottleId));
    }

    /**
     * 新增试剂瓶记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBottle:add')")
    @Log(title = "试剂瓶记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsReagentBottle insReagentBottle) {
        return toAjax(iInsReagentBottleService.save(insReagentBottle) ? 1 : 0);
    }

    /**
     * 修改试剂瓶记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBottle:edit')")
    @Log(title = "试剂瓶记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsReagentBottle insReagentBottle) {
        return toAjax(iInsReagentBottleService.updateById(insReagentBottle) ? 1 : 0);
    }

    /**
     * 删除试剂瓶记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBottle:remove')")
    @Log(title = "试剂瓶记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bottleIds}")
    public AjaxResult remove(@PathVariable Integer[] bottleIds) {
        return toAjax(iInsReagentBottleService.removeByIds(Arrays.asList(bottleIds)) ? 1 : 0);
    }
}
